﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using UDFDesign.GUIComponent.CustomControl;
using UDFDesign.GUIComponent.SupForm;

namespace UDFDesign.GUIComponent.CustomControl
{
    public class DrawingBoard : Panel
    {
        protected DesignableControl drawingControl;

        private DesignableControl selectedControl;

        private PropertiesPanel propPanel;

        private HashSet<ToolBoxButton> setToolBoxButton = new HashSet<ToolBoxButton>();

        public PropertiesPanel PropPanel
        {
            get { return propPanel; }
            set { propPanel = value; }
        }

        protected SupplierForm supForm;
        public SupplierForm SupForm
        {
            get { return supForm; }
            set {supForm= value;}
        }

        public void deleteControl(DesignableControl control)
        {
            this.Controls.Remove(control);
            SupForm.deleteControl(control);
        }

        public void setDrawingControl(DesignableControl control)
        {
            this.drawingControl = control;
        }

        public void releaseDrawingControl()
        {
            unUsingAllButton();
            this.drawingControl = null;
        }

        private void unUsingAllButton()
        {
            foreach (ToolBoxButton button in setToolBoxButton)
            {
                button.setUsing(false);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (drawingControl != null)
                {

                    unUsingAllButton();
                    Point location = new Point(e.X, e.Y);
                    if (this.ClientRectangle.Contains(location))
                    {
                        drawingControl.Location = new Point(e.X, e.Y);
                        addControl(drawingControl);
                        drawingControl = null;
                        
                    }


                }
            }

            base.OnMouseDown(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (selectedControl != null)
            {
                Pen dotPen = new Pen(Brushes.Black);
                dotPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
                e.Graphics.DrawRectangle(dotPen,
                        selectedControl.Left - 1,
                        selectedControl.Top - 1,
                        selectedControl.Width + 1,
                        selectedControl.Height + 1);
            }
        }

        public void addToolBoxButton(ToolBoxButton button)
        {
            setToolBoxButton.Add(button);
        }

        public void selectControl(DesignableControl control)
        {
            PropPanel.setControl(control.ControlMappingObj);
            selectedControl = control;
            this.Refresh();
        }

        public void unSelectControl()
        {
            PropPanel.setControl(null);
            selectedControl = null;
            this.Refresh();
        }

        public void addControl(DesignableControl control)
        {
            this.Controls.Add(control);
            this.selectControl(control);
            SupForm.addControl(control);
        }
    }
}
